package com.pocketsweet.ui;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
import com.pocketsweet.R;
import com.pocketsweet.ui.uilib.MyImageView;
import com.pocketsweet.ui.uilib.PicGallery;
import com.pocketsweet.ui.uilib.mySlideGalleryAdapter;

public class PictureViewFra extends Fragment {
	private static final String TAG = "PictureViewFra";
	private PicGallery gallery;
	// private ViewGroup tweetLayout; // 弹层
	private boolean mTweetShow = false; // 弹层是否显示

	private mySlideGalleryAdapter mAdapter;
	public static TextView tvNum;

	// private ProgressDialog mProgress;

	public mySlideGalleryAdapter getAdapter() {
		return mAdapter;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.picture_view, null);
		tvNum = (TextView) view.findViewById(R.id.tvNum);
		tvNum.setText(PictureViewActivity.currentIndex + 1 + "/"
				+ PictureViewActivity.ImageList.size());
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		// tweetLayout = (ViewGroup) view.findViewById(R.id.phote_info_layout);

		gallery = (PicGallery) view.findViewById(R.id.pic_gallery);
		gallery.setVerticalFadingEdgeEnabled(false);// 取消竖直渐变边框
		gallery.setHorizontalFadingEdgeEnabled(false);// 取消水平渐变边框
		gallery.setDetector(new GestureDetector(getActivity(),
				new MySimpleGesture()));
		mAdapter = new mySlideGalleryAdapter(getActivity());
//		Log.e("imagelist", PictureViewActivity.ImageList.toString());
		gallery.setAdapter(mAdapter);
		mAdapter.setData(PictureViewActivity.ImageList);
		gallery.setSelection(PictureViewActivity.currentIndex);
		gallery.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// Toast.makeText(getActivity(), "LongClick唤起复制、保存操作",
				// Toast.LENGTH_SHORT).show();
				return false;
			}
		});
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				tvNum.setText(gallery.getSelectedItemPosition() + 1 + "/"
						+ PictureViewActivity.ImageList.size());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		// mProgress = ProgressDialog.show(getActivity(),
		// null,getActivity().getString(R.string.loading));
	}

	private class MySimpleGesture extends SimpleOnGestureListener {
		// 按两下的第二下Touch down时触发
		public boolean onDoubleTap(MotionEvent e) {

			View view = gallery.getSelectedView();
			if (view instanceof MyImageView) {
				MyImageView imageView = (MyImageView) view;
				if (imageView.getScale() > imageView.getMiniZoom()) {
					imageView.zoomTo(imageView.getMiniZoom());
				} else {
					imageView.zoomTo(imageView.getMaxZoom());
				}

			} else {

			}
			return true;
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			// Logger.LOG("onSingleTapConfirmed",
			// "onSingleTapConfirmed excute");
			// mTweetShow = !mTweetShow;
			// tweetLayout.setVisibility(mTweetShow ? View.VISIBLE
			// : View.INVISIBLE);
			return true;
		}
	}

}
